﻿Imports System.Configuration
Imports System.Windows.Forms

Public Class Frm_SelectUrl
    Private Sub Frm_SelectDebugUrl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RestoreConfig()
    End Sub
    Private Url As String
    Public Overloads Function ShowDialog(o As IWin32Window) As String
        MyBase.ShowDialog(o)
        Return Url
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim SelectedItem As Config = Cmb_Url.SelectedItem
            If SelectedItem.IsNull Then
                Dim u = Cmb_Url.Text
                Dim utest As New Uri(u)
                Url = u
            Else
                Url = SelectedItem.Value
            End If
            Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RestoreConfig()
    End Sub
    Public Sub RestoreConfig()
        Cmb_Url.Items.Clear()
        Dim configFileMap As New ExeConfigurationFileMap With {
            .ExeConfigFilename = Sy.Environment.AppPath("debug.config")
        }
        Dim Config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
        Dim HasConfig = False
        For Each Key In Config.AppSettings.Settings.AllKeys
            Dim Value = Config.AppSettings.Settings(Key).Value
            Cmb_Url.Items.Add(New Config(Key, Value))
            HasConfig = True
        Next
        If HasConfig Then Cmb_Url.SelectedIndex = 0
    End Sub
    Private Class Config
        Public Key As String
        Public Value As String

        Public Sub New(key As String, value As String)
            Me.Key = key
            Me.Value = value
        End Sub
        Public Overrides Function ToString() As String
            Return $"{Key} [{Value}]"
        End Function
    End Class

    Private Sub Chk_Attr_CheckedChanged(sender As Object, e As EventArgs) Handles Chk_Attr.CheckedChanged
        Dim CheckBox As CheckBox = sender
        DebugTrace.Instance.OutPut = CheckBox.Checked
    End Sub

    Private Sub TitleBar1_ClickClose(sender As Object, e As FormClosingEventArgs) Handles TitleBar1.ClickClose
        Environment.Exit(0)
    End Sub
End Class